var util = require("../../utils/util.js"),
    {api} = require("../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        showXy: false,
        staticPath: app.globalData.staticPath+'/static/wx/images',
        currentLogin: 'admin', //admin 账号密码 wx 微信手机登录 mobile 手机号码登陆
        wxMobile: null,
        adminForm: {
            userName: '',
            passWord: ''
        },
        mobileFrom: {
            mobile: '',
            code: ''
        },
        isShowPassWord: false,
        downNum:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.hideShareMenu();
    },

    onChange() {
        this.setData({
            checked: !this.data.checked,
        });
    },
    changeLogin(e) {
        let type = e.currentTarget.dataset.type;
        this.setData({
            currentLogin: type,
            wxMobile: null,
            adminForm: {
                userName: '',
                passWord: ''
            },
            mobileFrom: {
                mobile: '',
                code: ''
            },
            isShowPassWord: false
        })
    },
    goView(e) {
        let type = e.currentTarget.dataset.type;
        wx.navigateTo({
            url: '/pages/login/userWebView?type=' + type,
        })
    },
    showPassword() {
        console.log('22')
        this.setData({
            isShowPassWord: !this.data.isShowPassWord
        })
    },
    submit(e) {
        if (this.data.checked) {
            if (e.detail.errMsg == "getPhoneNumber:ok") {
                
                wx.login({
                  success: (res) => {
                    let share_id=wx.getStorageSync('share_id')  
                    let data = {
                        code: res.code,
                        iv: e.detail.iv,
                        encrypted: e.detail.encryptedData
                    };
                    if(share_id && util.regularConfig.isNumber(Number(share_id))){
                        data.share_id=share_id;
                    }
                    api.getMobile(data).then(res => {
                        wx.setStorageSync('userMobile', res.data.mobile)
                        wx.removeStorageSync('share_id')
                        this.setUserStorInfo();
                    })
                  },
                })
                
            } else {
                wx.showModal({
                    title: '提示',
                    content: '您的手机号码获取出现了错误',
                    cancelText: '知道了',
                    showCancel: false
                })
            }
        }else{
            this.setData({showXy:true})
        }
    },
    agreewith(e){
        this.setData({showXy:false,checked:true})
        this.submit(e);
    },
    agreewithNo(){
        this.setData({showXy:false,checked:false})
    },
    getMobile(e) {
        this.data.mobileFrom.mobile = e.detail.value;
        this.setData({
            mobileFrom: this.data.mobileFrom
        })
    },
    getMobileCode(e) {
        this.data.mobileFrom.code = e.detail.value;
        this.setData({
            mobileFrom: this.data.mobileFrom
        })
    },
    getPaswordMobile(e) {
        this.data.adminForm.userName = e.detail.value;
        this.setData({
            adminForm: this.data.adminForm
        })
    },
    getPasword(e) {
        this.data.adminForm.passWord = e.detail.value;
        this.setData({
            adminForm: this.data.adminForm
        })
    },
    sendCode(){
        if (!util.regularConfig.mobile.test(this.data.mobileFrom.mobile)) {
            return wx.showToast({
                title: '电话号码格式不正确',
                icon: 'none'
            });
        }
        
        api.sendCode({countryCode:'86',mobile:this.data.mobileFrom.mobile}).then(res=>{
            wx.showToast({ icon:'success',title: '验证码发送成功'})
            this.downTime()
        })
    },
    downTime(){
        this.setData({downNum:60})
        this.timers=setInterval(()=>{
            if(this.data.downNum == 0){
                clearInterval(this.timers);
                return;
            }
            this.data.downNum--;
            this.setData({downNum:this.data.downNum})
        },1000)
    },
    setUserStorInfo(data){
        wx.showToast({ icon:'success',title: '登录成功'})
        setTimeout(()=>{
            let page=getCurrentPages();
            if(page.length == 1){
                wx.switchTab({
                  url: '/pages/index/index',
                })
            }else{
                wx.navigateBack();
            }
        },1500)
    }
})